package com.lubase.wfengine.remote.impl;

import com.alibaba.fastjson.JSON;
import com.lubase.orm.extend.ExtendAppLoadCompleteService;
import com.lubase.wfengine.auto.entity.WfCallbackEntity;
import com.lubase.wfengine.model.OpenEventModel;
import com.lubase.wfengine.remote.SendEventToRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@ConditionalOnProperty(name = "lubase.wf-engine.mq-type", havingValue = "rabbitmq")
public class SendEventToRemoteServiceRabbitMQImpl extends SendEventToRemoteService {
    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    AmqpAdmin amqpAdmin;

    @Value("${lubase.wf-engine.engine-topic:LUBASE_WF_ENGINE_TOPIC}")
    private String wfEngineTopic;

    @Value("${lubase.wf-engine.update-table-topic:LUBASE_WF_UPDATE_TABLE_TOPIC}")
    private String wfUpdateTableTopic;

    @Value("${lubase.wf-engine.public-topic:LUBASE_WF_PUBLIC_TOPIC}")
    private String wfPublicTopic;

    @Autowired
    CacheManager cacheManager;
    @Override
    public void sendWorkFlowEngineEvent(String serviceId, String dataId) {
        Cache cache = cacheManager.getCache(cacheName);
        String key = String.format("engine:%s:%s", serviceId, dataId);
        List<String> list = null;
        if (cache != null) {
            list = cache.get(key, List.class);
        } else {
            list = new ArrayList<>();
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        for (String msg : list) {
            try {
                amqpTemplate.convertAndSend(wfEngineTopic, msg);
            } catch (Exception exception) {
                log.error("流程引擎：流程事件发送MQ发生错误：" + msg, exception);
            }
        }
        cache.evict(key);
    }

    @Override
    public void sendUpdateBisTableEvent(String serviceId, String dataId, WfCallbackEntity entity) {
        if (entity == null) {
            return;
        }
        String msg = JSON.toJSONString(entity);
        try {
            amqpTemplate.convertAndSend(wfUpdateTableTopic, msg);
        } catch (Exception exception) {
            log.error("流程引擎：更新业务表事件发送MQ发生错误：" + msg, exception);
        }
    }

    @Override
    public void sendWorkFlowOpenEvent(OpenEventModel model) {
        if (model == null) {
            return;
        }
        String serviceId = model.getServiceId();
        String msg = JSON.toJSONString(model);
        try {
            amqpTemplate.convertAndSend(wfPublicTopic, serviceId, msg);
        } catch (Exception exception) {
            log.error("流程引擎：公开事件发送MQ发生错误：" + msg, exception);
        }
    }

    @Override
    public void sendWorkFlowOpenEventToMqFromCache(String serviceId, String dataId) {
        Cache cache = cacheManager.getCache(cacheName);
        String key = String.format("open:%s:%s", serviceId, dataId);
        List<String> list = null;
        if (cache != null) {
            list = cache.get(key, List.class);
        } else {
            list = new ArrayList<>();
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        for (String msg : list) {
            try {
                amqpTemplate.convertAndSend(wfPublicTopic, serviceId, msg);
            } catch (Exception exception) {
                log.error("流程引擎：公开事件发送MQ发生错误：" + msg, exception);
            }
        }
        cache.evict(key);
    }

}
